
function getTicket() { 

  /* 
  
  lightAppJssdk.user.getTicket ({
    success:function(data){ //成功回调
    },
    fail:function(data){ //错误返回
    }
    });
*/
  /* data:{
    ticket:'',用户票据
    result:'true/false',成功标识
    message:'',错误时的描述
  }
   */
  return Promise.resolve({
    ticket: '',
    result: 'true',
    message:'',
  })//
}


//通过票据获取 tokenSNO 接口
/* data:{
    ticket:'',用户票据
    timestamp:'',时间戳
    sign:'',签名（票据+密钥+时间戳） // gjzwfwpt@201811!
  }
   */
function getTokenSNO({ ticket ,timestamp,sign}) { 
  /* data:{
    tokenSNO:'',票据
    result:'true/false',成功标识
    errormsg:'',错误时的描述
  }
   */
  return Promise.resolve({
    tokenSNO:'',//票据
    result:'true',//成功标识
    errormsg:''//,错误时的描述
  })
}

function loginapp() { 
  lightAppJssdk.user.loginapp ({
    success:function(data){ //成功回调
    },
    fail:function(data){ //错误返回
    }
  });
}

export async function login() {
  let success, error;
  const p = new Promise((resolve, reject) => {
    success=resolve
    error=reject
  });

  const data = {}
  try {
    res = await getTicket() || {};
    const { result,ticket } = res;
    if (result === 'true') {
      data = {ticket}
    } else { 
      const { result, ticket } = await loginapp();
      if (result === 'true') { 
        data = {ticket}
      }
    }
  } catch (e) {
    reject('getTicket',e)
  }

  try {
    if (data.ticket) { 
      const timestamp= new Date().getTime() + ''
      data = {
        ...data,
        timestamp,
        sign:data.ticket+"gjzwfwpt@201811!"+timestamp
      }
      
      const { tokenSNO = '' ,result} = await getTokenSNO(data) || {}
      if (result === 'true') { 
        success(tokenSNO)
      }
    }
  } catch (e) { 
    reject('getTokenSNO',e)
  }

  return p

 });
